/**
 * @author Tobias Boehm
 *
 * 16.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.UUID;

import org.anddev.andengine.extension.tbsengine.player.BasePlayer;

/**
 * This is the base class for all client messages. It contains a member 
 * variable for a player ID. Every client message needs at least this ID to
 * identify it's source.
 * <p>
 * If you want to create custom-client messages you should extend this class. 
 * 
 * @author Tobias Boehm
 *
 */
public abstract class AbsClientMessage extends SecureClientMessage implements
		DefaultTBSclientMessageFlags {

	protected UUID mPlayerID = null;

		
	@Override
	protected void onReadDecryptedTransmissionData(
			DataInputStream pDataInputStream) throws IOException {
		
		this.mPlayerID = UUID.fromString(pDataInputStream.readUTF());

		this.onReadTransmissionClientData(pDataInputStream);
	}
	
	protected abstract void onReadTransmissionClientData(final DataInputStream pDataInputStream) throws IOException;
	
	@Override
	protected void onWriteNonEncryptedTransmissionData(
			DataOutputStream pDataOutputStream) throws IOException {
		
		pDataOutputStream.writeUTF(this.mPlayerID.toString());

		this.onWriteTransmissionClientData(pDataOutputStream);
	}
	
	protected abstract void onWriteTransmissionClientData(final DataOutputStream pDataOutputStream) throws IOException;

	/**
	 * @return the mPLayerID
	 */
	public UUID getPlayerID() {
		return mPlayerID;
	}

	/**
	 * @param mPlayerID the mPLayerID to set
	 */
	public void setPlayer(final BasePlayer pPlayer) {
		this.mPlayerID = pPlayer.getPlayerID();
	}
}
